package kcn.ip21.dbservice;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import kcn.ip21.utils.DButils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import sun.jdbc.odbc.JdbcOdbcDriver;

public class DbService {
	public int save(Object cus) {
		Session session = DButils.getSession();
		try {
			session.getTransaction().begin();
			// session.save(cus);
			session.save(cus);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return 0;
	}

	public Map getdatafromIP21(List<String> ip21tags, String time) {
		Map mp = new HashMap();
		String username = null;
		String password = null;
		String sql = null;
		try {
			DriverManager.registerDriver(new JdbcOdbcDriver());
			// IP21DataSource
			String url = "jdbc:odbc:IP21DataSource";
			Connection con = DriverManager.getConnection(url, username,
					password);
			Statement st = con.createStatement();
			for (int i = 0; i < ip21tags.size(); i++) {
				ResultSet rs = null;
				String tagname = ip21tags.get(i);
				sql = "Select IP_TREND_VALUE from \""
						+ tagname
						+ "\" where IP_TREND_TIME in (select MAX(IP_TREND_TIME) from \""
						+ tagname + "\" where IP_TREND_TIME<'" + time + "')";
//				System.out.println(sql);
				rs = st.executeQuery(sql);
				if (rs.next())
					mp.put(tagname, rs.getString(1));
//				System.out.println(tagname+" "+rs.getString(1));
				rs.close();
			}
			st.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block\
			System.out.println(e.getSQLState());
			e.printStackTrace();
		}
		return mp;
	}

	public int update(Object cus) {
		Session session = DButils.getSession();
		try {
			session.getTransaction().begin();
			// session.save(cus);
			session.update(cus);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return 0;
	}

	public Object readbyid(int id) {
		Session session = DButils.getSession();
		session.getTransaction().begin();
		// cus=session.get(arg0, arg1);
		session.getTransaction().commit();
		session.close();
		return null;
	}

	public List readAll() {
		Session session = null;
		List list = null;
		System.out.println("111");
		try {
			System.out.println("222");
			session = DButils.getSession();
			System.out.println("333");
			session.getTransaction().begin();
			list = session.createQuery("from Object").list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	public int getTotal(String SQL) {
		Session ss = null;
		int total = 0;
		try {
			ss = DButils.getSession();
			ss.getTransaction().begin();
			System.out.println(ss);
			total = Integer.parseInt(ss.createQuery("select count(*) " + SQL)
					.iterate().next().toString());
			ss.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ss.close();
		System.out.println(total);
		return total;
	}

	//
	public List getbypage(int start, int length, String SQL) {
		Session session = null;
		List list = null;
		session = DButils.getSession();
		try {
			session.getTransaction().begin();
			Query q = session.createQuery(SQL);
			q.setFirstResult(start);
			q.setMaxResults(length);
			list = q.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return list;
	}

	public int delete(Object cus) {
		Session session = DButils.getSession();
		try {
			session.getTransaction().begin();
			session.delete(cus);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return 0;
	}
}
